package api.betfairAPI;

import com.betfair.www.publicapi.types.global.v3.BFEvent;
import com.betfair.www.publicapi.types.global.v3.EventType;
import com.betfair.www.publicapi.types.global.v3.MarketSummary;
import com.betfair.www.publicapi.types.global.v3.MarketTypeEnum;

import java.text.SimpleDateFormat;

/**
 * User: Darren
 * Date: 19/01/13
 * Time: 4:54 PM
 */
public class BetfairStringHelper {
    private String stringDivider = ", ";
    private String selectionDividerOpen = "[ ";
    private String selectionDividerClose = " ]";
    private StringBuilder sb = new StringBuilder();
    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm") ;
    private String eventFieldNames;
    private String marketFieldNames;

    public BetfairStringHelper() {
        initiEventFieldNames();
        initiMarketFieldNames();
    }

    public BetfairStringHelper(String stringDivider, String selectionDividerOpen,
                               String selectionDividerClose, String simpleDatePattern) {
        this.stringDivider = stringDivider;
        this.selectionDividerOpen = selectionDividerOpen;
        this.selectionDividerClose = selectionDividerClose;
        this.sdf.applyPattern(simpleDatePattern);
        initiEventFieldNames();
        initiMarketFieldNames();
    }

    private void initiEventFieldNames() {
        eventFieldNames = getStringBuilder().append("Event ID")
                .append(stringDivider).append("Event Name")
                .append(stringDivider).append("Event Type ID")
                .append(stringDivider).append("Menu Level")
                .append(stringDivider).append("Order Index")
                .append(stringDivider).append("Start Time")
                .append(stringDivider).append("Time Zone").toString();
    }
    private void initiMarketFieldNames() {
        marketFieldNames = getStringBuilder().append("Event Type ID")
                .append(stringDivider).append("Exch ID")
                .append(stringDivider).append("Event Parent ID")
                .append(stringDivider).append("Market ID")
                .append(stringDivider).append("Market Name")
                .append(stringDivider).append("Market Type")
                .append(stringDivider).append("Menu Level")
                .append(stringDivider).append("Order Index")
                .append(stringDivider).append("Start Time")
                .append(stringDivider).append("Time Zone")
                .append(stringDivider).append("Venue")
                .append(stringDivider).append("Bet Delay")
                .append(stringDivider).append("Number of Winners").toString();
    }

    public String getEventFieldNames() {
        return eventFieldNames;
    }
    public String getMarketFieldNames() {
        return marketFieldNames;
    }

    private StringBuilder getStringBuilder() {
        return sb.delete(0, sb.length());
    }

    public String eventTypeToString(EventType eventType) {
        StringBuilder sb = getStringBuilder();
        sb.append(eventType.getName());
        if (eventType.getExchangeId() == 1) {
            sb.append(stringDivider).append("UK exchange");
        } else if (eventType.getExchangeId() == 2) {
            sb.append(stringDivider).append("AUS exchange");
        }
        if (eventType.getNextMarketId() > 0) {
            sb.append(stringDivider).append("event starting soon");
        }
        return sb.toString();
    }

    public String[] eventTypesToStrings(EventType[] eventTypes) {
        String[] strings = new String[eventTypes.length];
        for (int i = 0; i < eventTypes.length; ++i) {
            strings[i] = eventTypeToString(eventTypes[i]);
        }
        return strings;
    }

    public String bFEventToString(BFEvent bfEvent) {
        StringBuilder sb = getStringBuilder();
        return sb.append(bfEvent.getEventId())
                .append(stringDivider).append(bfEvent.getEventName())
                .append(stringDivider).append(bfEvent.getEventTypeId())
                .append(stringDivider).append(bfEvent.getMenuLevel())
                .append(stringDivider).append(bfEvent.getOrderIndex())
                .append(stringDivider).append(sdf.format(bfEvent.getStartTime().getTime()))
                .append(stringDivider).append(bfEvent.getTimezone()).toString();
    }

    public String[] bFEventToString(BFEvent[] bfEvents) {
        String[] strings = new String[bfEvents.length];
        for (int i = 0; i < bfEvents.length; ++i) {
            strings[i] = bFEventToString(bfEvents[i]);
        }
        return strings;
    }

    public String marketSummaryToString(MarketSummary marketSummary) {
        StringBuilder sb = getStringBuilder();
        sb.append(marketSummary.getEventTypeId())
                .append(stringDivider).append(marketSummary.getExchangeId())
                .append(stringDivider).append(marketSummary.getEventParentId())
                .append(stringDivider).append(marketSummary.getMarketId())
                .append(stringDivider).append(marketSummary.getMarketName())
                .append(stringDivider);
        if (marketSummary.getMarketType() == MarketTypeEnum.O) {
            sb.append("Odds");
        } else if (marketSummary.getMarketType() == MarketTypeEnum.L) {
            sb.append("Line");
        } else if (marketSummary.getMarketType() == MarketTypeEnum.R) {
            sb.append("Range");
        } else if (marketSummary.getMarketType() == MarketTypeEnum.A) {
            sb.append("Asian Handicap").append(stringDivider).append(marketSummary.getMarketTypeVariant());
        } else {
            sb.append("N/A");
        }
        sb.append(stringDivider).append(marketSummary.getMenuLevel())
                .append(stringDivider).append(marketSummary.getOrderIndex())
                .append(stringDivider).append(sdf.format(marketSummary.getStartTime().getTime()))
                .append(stringDivider).append(marketSummary.getTimezone())
                .append(stringDivider).append(marketSummary.getVenue())
                .append(stringDivider).append(marketSummary.getBetDelay())
                .append(stringDivider).append(marketSummary.getNumberOfWinners());
        return sb.toString();
    }

    public String[] marketSummaryToString(MarketSummary[] marketSummaries) {
        String[] strings = new String[marketSummaries.length];
        for (int i = 0; i < marketSummaries.length; ++i) {
            strings[i] = marketSummaryToString(marketSummaries[i]);
        }
        return strings;
    }

    public String[] stringsToMenuItems(String[] strings) {
        String[] menuItems = new String[strings.length];
        StringBuilder sb;
        for (int i = 0; i < strings.length; ++i) {
            sb = getStringBuilder();
            menuItems[i] = sb.append(selectionDividerOpen).append(i).append(selectionDividerClose)
                    .append(stringDivider).append(strings[i]).toString();
        }
        return menuItems;
    }

    public String[] stringsToMenuItems(String[] strings, int firstMenuItemIndex) {
        String[] menuItems = new String[strings.length];
        StringBuilder sb;
        for (int i = 0; i < strings.length; ++i) {
            sb = getStringBuilder();
            menuItems[i] = sb.append(selectionDividerOpen).append(String.valueOf(firstMenuItemIndex + i))
                    .append(selectionDividerClose).append(stringDivider).append(strings[i]).toString();
        }
        return menuItems;
    }
}
